<template>
    <teleport to='body' v-if="dialogExit">
        <div class="teleportStyle">
            <transition name="dialogShadeName">
                <div class="shadeStyle" v-if="dialogShadeExit"></div>
            </transition>
            <transition name="dialogContentName">
                <div class="dialogStyle" v-if="dialogContentExit" id="testID">
                    <span style="margin-left:5%;" v-if="talkTitle == null">取一个好听的会话名字吧！</span>
                    <span style="margin-left:5%;" v-else>您需要更换会话名字吗？</span>
                    <input type="text" class="dialogInputStyle" v-model="dialogInputText" 
                    :style="validateDialog ? 'border-bottom: 1px solid rgb(63, 130, 255);':''">
                    <div class="dialogBtnListtyle">
                        <span class="dialogCloseStyle" @click="dialogCloseBtn">关闭</span>
                        <span class="dialogSkipStyle" @click="dialogSkipBtn" v-if="talkTitle == null">跳过</span>
                        <span class="dialogConfirmStyle" @click="dialogConfirmBtn"
                        :style="validateDialog ? 'background-color: rgba(36, 96, 209,1);':''">确定</span>
                    </div>
                </div>
            </transition>
        </div>
    </teleport>
</template>
<script setup>
import { onMounted, ref,getCurrentInstance, reactive, onBeforeUnmount, watch,nextTick} from 'vue'
import useTalkStore from '@/store/modules/talk'
import _bus from '@/utils/bus'
const { proxy } = getCurrentInstance()
defineEmits(['setTalkTitle'])
defineProps(['talkTitle'])
onMounted(() => {
    _bus.on('bus_clickCreateBtn',()=>{
        clickCreateBtn()
    })
})
onBeforeUnmount(()=>{
    _bus.off('bus_clickCreateBtn')
})

const dialogExit = ref(false)
const dialogShadeExit = ref(false)
const dialogContentExit = ref(false)
const clickCreateBtn = (val)=>{
    dialogExit.value = true
    dialogInputText.value = val
    nextTick(()=>{
        dialogShadeExit.value = true
        dialogContentExit.value = true
    })
}

let dialogInputText = ref()
let validateDialog = ref(false)
watch(dialogInputText,(newVal,oldVal)=>{
    if(newVal != null){
        dialogInputText.value = newVal.slice(0,7)
        validateDialog.value = Boolean(dialogInputText.value.length >= 3)
    }else{
        validateDialog.value = false
    }
})
let talkStore = useTalkStore()
// 整个弹出框入场、然后输入框聚焦样式的转变，然后点阴影部分退出弹出框
const dialogSkipBtn = ()=>{
    talkStore.setTalkTitleStatus(false)
    dialogCloseBtn()
}
const dialogConfirmBtn = ()=>{
    if(validateDialog.value){
        dialogCloseBtn()
        writeTitleName(dialogInputText.value)
    }
}
const dialogCloseBtn = ()=>{
    dialogContentExit.value = false
    dialogShadeExit.value = false
    setTimeout(() => {
        dialogInputText.value = null
        dialogExit.value = false
    }, 200);
}
const writeTitleName = (val)=>{
    localStorage.setItem('talkName',val)
    proxy.$emit('setTalkTitle',val)
}
defineExpose({
    clickCreateBtn,
})
</script>
<style scoped lang="scss">
.teleportStyle{
    z-index: 999;
    user-select: none;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    position: absolute;
    overflow: hidden;
    .shadeStyle{
        width: 100%;
        height: 100%;
        background-color: rgba(34, 34, 34, 0.616);
    }
    .dialogStyle{
        border-radius: 6px;
        padding-top: 4%;
        position: absolute;
        height: 11rem;
        width: 60%;
        left: 20%;
        top: 5rem;
        background-color: white;
        .dialogInputStyle{
            margin-top: 20%;
            left: 10%;
            width: 70%;
            position: absolute;
            font-size: 20px;
            border: 0px;
            border-bottom: 1px solid rgb(172, 172, 172);
            outline: none;
            padding-bottom: 3px;
            transition: all .2s ease-in-out;
        }
        .dialogBtnListtyle{
            width: 100%;
            position: absolute;
            bottom: 10%;
            display: flex;
            justify-content: space-between;
            span{
                flex: 1;
                text-align: center;
                margin: 0 5%;
                padding: 5px;
                border-radius: 5px;
                padding-left: 10px;
                padding-right: 10px;
                color: white;
            }
            .dialogCloseStyle{
                background-color: rgb(182, 106, 36);
            }
            .dialogSkipStyle{
                background-color: rgb(24, 151, 45);
            }
            .dialogConfirmStyle{
                transition: all .2s ease-in-out;
                background-color: rgba(36, 96, 209,.5);
            }
        }
    }
}
.dialogContentName-enter-active {
    transition: all .2s ease-out;
}

.dialogContentName-leave-active {
    transition: all .2s cubic-bezier(1, 0.5, 0.8, 1);
}

.dialogContentName-enter-from{
    transform: translateY(-100%);
    opacity: 0;
}
.dialogContentName-leave-to{
    transform: translateX(100%);
    opacity: 0;
}
.dialogShadeName-enter-active,.dialogShadeName-leave-active{
    transition: all .2s ease-out;
}
.dialogShadeName-enter-from,.dialogShadeName-leave-to{
    opacity: 0;
}
</style>